package run.liuli.tran.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;
import run.liuli.tran.user.dao.UserDao;
import run.liuli.tran.user.enmu.ToCouponStatus;
import run.liuli.tran.user.pojo.UserCouponTran;
import run.liuli.tran.user.pojo.User;
import run.liuli.tran.user.service.UserCouponTranService;
import run.liuli.tran.user.service.UserService;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private UserCouponTranService userCouponTranService;

    @Override
    @Transient
    public void post(Long uid, Long money) {
        User user = userDao.selectById(uid);
        if(Objects.isNull(user))
            throw new RuntimeException("用户不存在");
        if(user.getMoney()<money)
            throw new RuntimeException("余额不足");
        //用户扣钱
        user.setMoney(user.getMoney()-money);
        userDao.updateById(user);
        userCouponTranService.save(new UserCouponTran(user.getUid(),money, ToCouponStatus.USER_COUPON_NO_SEND.getCode()));
    }
}
